/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import java.util.List;

import com.cleangwt.bootstrap.client.SystemMessages;
import com.cleangwt.bootstrap.client.style.Styles;
import com.cleangwt.uibinder.client.TemplateSupport;
import com.cleangwt.user.client.HashTokenizer;
import com.cleangwt.user.shared.Link;
import com.cleangwt.user.shared.Links;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.OListElement;
import com.google.gwt.user.client.ui.Widget;

/**
 * The breadcrubm to indicate the current page's location within a navigational hierarchy..
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 * @see http://getbootstrap.com/components/#breadcrumbs
 */
public class Breadcrumb extends Widget {

  private static final String DEFAULT_STYLENAME = "breadcrumb";

  /**
   * Creates a breadcrumbs that wraps an existing &lt;ol&gt; element of template.
   * 
   * @param template the template
   * @param element the element to be wrapped
   */
  public static Breadcrumb wrap(TemplateSupport template, Element element) {
    Breadcrumb widget = new Breadcrumb(element);
    template.wrap(widget);
    return widget;
  }

  /**
   * Creates a breadcrumb
   */
  public Breadcrumb() {
    this.setElement(Document.get().createOLElement());
    setStyleName(DEFAULT_STYLENAME);
  }

  /**
   * This constructor may be used by subclasses to explicitly use an existing element. This element
   * must be a &lt;div&gt; element.
   * 
   * @param element the element to be used
   */
  protected Breadcrumb(Element element) {
    assert element.getTagName().equalsIgnoreCase(OListElement.TAG);
    this.setElement(element);
  }

  /**
   * Sets the links for the current page's location
   * 
   * @param links the links for the current page's location
   */
  public void setLinks(List<Link> links) {
    this.getElement().setInnerHTML("");
    int last = links.size() - 1;

    addItem(SystemMessages.Impl.home(), "");
    for (int i = 0; i < last; i++) {
      Link link = Links.linkToLeaf(links.get(i));
      addItem(link.getName(), link.getValue());
    }
    addItem(links.get(last).getName(), null);
  }

  /**
   * Adds an item.
   * 
   * @param text the text
   * @param pageName the page name
   */
  private void addItem(String text, String pageName) {
    Element item = Document.get().createLIElement();
    getElement().appendChild(item);
    if (pageName == null) {
      item.setInnerHTML(text);
      item.setClassName(Styles.ACTIVE);
    } else {
      AnchorElement anchor = Document.get().createAnchorElement();
      anchor.setInnerHTML(text);
      anchor.setHref(HashTokenizer.packPageHash(pageName));
      item.appendChild(anchor);
    }
  }

}
